package cn.edu.tsinghua.cic.ccss.qo;

import java.util.Date;

import org.forten.core.dao.rdb.hibernate.QueryInfo;
import org.forten.core.dao.rdb.query.Operator;
import org.forten.core.dao.rdb.query.hql.expression.Between;
import org.forten.core.dao.rdb.query.hql.expression.BetweenFields;
import org.forten.core.dao.rdb.query.parameter.Param;
import org.forten.core.dao.rdb.query.parameter.Type;
import org.forten.core.qo.QueryEntity;
import org.forten.core.qo.QueryField;
import org.forten.core.qo.QueryField.MatchType;
import org.forten.core.qo.QueryObject;
import org.forten.utils.common.DateUtil;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import cn.edu.tsinghua.cic.ccss.entity.Course;

@QueryEntity(Course.class)
@Component("courseSelectAdminQo")
@Scope("prototype")
public class CourseSelectAdminQo implements QueryObject{
	@QueryField(fieldName = {"name"}, paramName = {"name"}, operator = Operator.LIKE , matchMode = MatchType.ANYWHERE)
	private String name;
	@QueryField(fieldName ={"teacherName"}, paramName = {"teacherName"}, operator = Operator.LIKE, matchMode = MatchType.ANYWHERE)
	private String teacherName;
	private String start;
	private String end;
	
	@Override
	public void clean() {
		teacherName = null;
		name = null;
		start = null;
		end = null;
	}

	@Override
	public void beforeBuildQueryInfo() {
		
	}

	@Override
	public QueryInfo afterBuildQueryInfo(QueryInfo qi) {
		Date currTime = DateUtil.currentTime();
		String currTimeStr = DateUtil.convertDateToString(currTime,
				"yyyy/MM/dd HH:mm");
		Param finalCurrTime = Param.getCommonParam("begin", currTimeStr,
				Type.STRING);
		finalCurrTime.setPrefix("to_date(");
		finalCurrTime.setPostfix(",'yyyy/MM/dd HH24:MI')");
		BetweenFields defaultBetweenTime = BetweenFields.between(
				"startTimeChoose", "teachTime", finalCurrTime);
		qi.addExpressionsToWhere(defaultBetweenTime);
		
		Between betweenTime = null;
		if(start !=null && "".equals(end) && !"".equals(start)){
			Param startParam = Param.getCommonParam("start", start+"0000", Type.STRING);
			Param endParam = Param.getCommonParam("end", DateUtil.convertDateToString
					(DateUtil.currentTime(),"yyyyMMdd2359"), Type.STRING);
			startParam.setPrefix("to_date(");
			endParam.setPrefix("to_date(");
			startParam.setPostfix(",'yyyyMMddHH24MI')");
			endParam.setPostfix(",'yyyyMMddHH24MI')");
			betweenTime = Between.between("teachTime", startParam, endParam);
			
		} else 
		//若起始时间和结束时间均为null
		if(start != null && end != null && !"".equals(start) && !"".equals(end)){
			Param startParam = Param.getCommonParam("start", start+" 00:00", Type.STRING);
			Param endParam = Param.getCommonParam("end", end+" 23:59", Type.STRING);
			startParam.setPrefix("to_date(");
			endParam.setPrefix("to_date(");
			startParam.setPostfix(",'yyyyMMdd HH24:MI')");
			endParam.setPostfix(",'yyyyMMdd HH24:MI')");
			betweenTime = Between.between("teachTime", startParam, endParam);
		}
		
		if(betweenTime != null){
			qi.addExpressionsToWhere(betweenTime);
		}
		
		return qi;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getTeacherName() {
		return teacherName;
	}

	public void setTeacherName(String teacherName) {
		this.teacherName = teacherName;
	}

	public String getStart() {
		return start;
	}

	public void setStart(String start) {
		this.start = start;
	}

	public String getEnd() {
		return end;
	}

	public void setEnd(String end) {
		this.end = end;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((end == null) ? 0 : end.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + ((start == null) ? 0 : start.hashCode());
		result = prime * result
				+ ((teacherName == null) ? 0 : teacherName.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		CourseSelectAdminQo other = (CourseSelectAdminQo) obj;
		if (end == null) {
			if (other.end != null)
				return false;
		} else if (!end.equals(other.end))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (start == null) {
			if (other.start != null)
				return false;
		} else if (!start.equals(other.start))
			return false;
		if (teacherName == null) {
			if (other.teacherName != null)
				return false;
		} else if (!teacherName.equals(other.teacherName))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "CourseSelectAdminQo [name=" + name + ", teacherName="
				+ teacherName + ", start=" + start + ", end=" + end + "]";
	}

}
